package com.yl.module.property.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yl.common.annotation.CacheResult;
import com.yl.common.constant.CacheConstants;
import com.yl.common.constant.Constants;
import com.yl.common.utils.NumberUtils;
import com.yl.common.utils.TreeUtils;
import com.yl.module.property.domain.Property;
import com.yl.module.property.domain.vo.ColorRgbVO;
import com.yl.module.property.domain.vo.PropertyTypeTreeVO;
import com.yl.module.property.mapper.PropertyTypeMapper;
import com.yl.module.property.service.PropertyTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.yl.module.property.domain.vo.PropertyTypeVO;
import com.yl.module.property.domain.dto.PropertyTypeDTO;
import com.yl.module.property.domain.PropertyType;
import cn.hutool.core.bean.BeanUtil;

import java.awt.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


import java.util.*;
import java.util.function.Function;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.CollectionUtils;

/**
 * 资产类型表 Service实现类
 * @author makejava
 * @date 2024-10-23 15:54:16
 */
@Service
@Slf4j
public class PropertyTypeServiceImpl extends ServiceImpl<PropertyTypeMapper, PropertyType> implements PropertyTypeService {



   /**
	 * 查询所有
	 */
	@Override
	public List<PropertyTypeVO> getAll(){
		LambdaQueryWrapper<PropertyType> lqw = new LambdaQueryWrapper<>();
		lqw.in(PropertyType::getDeleteFlag,Constants.NO);
		List<PropertyType> list = this.list(lqw);

		return BeanUtil.copyToList(list, PropertyTypeVO.class);
	}

	@Override
	public Map<Integer,PropertyType> getIdAndObjAll(){
		return this.list().stream().collect(Collectors.toMap(PropertyType::getId, e->e));
	}

	@Override
	public List<PropertyTypeTreeVO> getTree(Integer id){
		List<PropertyType> list = this.list(new LambdaQueryWrapper<PropertyType>()
				.eq(PropertyType::getDeleteFlag, Constants.NO));
		if(list.isEmpty()){
			return new ArrayList<>();
		}
		List<PropertyTypeTreeVO> list2 = BeanUtil.copyToList(list, PropertyTypeTreeVO.class);

		List<PropertyTypeTreeVO> tree = TreeUtils.createTree(
				list2,
				TreeUtils.TreeFieldEnum.ID.getCode(),
				TreeUtils.TreeFieldEnum.PARENT_ID.getCode(),
				TreeUtils.TreeFieldEnum.CHILD.getCode(),
				true
		);

		List<PropertyTypeTreeVO> child = new ArrayList<>();
		for(PropertyTypeTreeVO i:tree){
			if(i.getId().equals(id)){
				child = i.getChild();
				break;
			}
		}

		return child;
	}

	/**
     * 分页查询
     */
	@Override
    public Page<PropertyType> getPage(PropertyTypeDTO dto){
		//创建分页对象
		Page<PropertyType> page = new Page<PropertyType>(dto.getPageNum(), dto.getPageSize());
		//dto查询条件转分页条件
		LambdaQueryWrapper<PropertyType> lqw = new LambdaQueryWrapper<>();
		return this.getBaseMapper().selectPage(page, lqw);
	}

	/**
	 * 通过主键查询
	 */
	@Override
	public PropertyTypeVO getByKey(Integer id){
		PropertyType entity = super.getById(id);
		return BeanUtil.copyProperties(entity, PropertyTypeVO.class);
    }

	/**
	 * 添加
	 */
	@Override
	public Integer add(PropertyTypeDTO dto){
		PropertyType propertyType = BeanUtil.copyProperties(dto, PropertyType.class);
		this.save(propertyType);
		return propertyType.getId();
	}

	/**
	 * 通过主键修改
	 */
	@Override
	public void upd(PropertyTypeDTO dto){
		PropertyType propertyType = BeanUtil.copyProperties(dto, PropertyType.class);
	    this.updateById(propertyType);
	}

	@Override
	public List<PropertyTypeTreeVO> getComponentMaintainState(){
		LambdaQueryWrapper<PropertyType> lqw = new LambdaQueryWrapper<>();
		lqw.in(PropertyType::getDeleteFlag,Constants.NO);
		lqw.last("and find_in_set("+PropertyType.IdEnum.ID_4.getCode()+",parent_ids) > 0");
		List<PropertyType> list = this.list(lqw);
		if(CollUtil.isEmpty(list)){
			return new ArrayList<>();
		}

		//处理保养状态
		List<PropertyTypeTreeVO> list2 = BeanUtil.copyToList(list, PropertyTypeTreeVO.class);
		LocalDateTime currentDate = LocalDateTime.now();
		for(PropertyTypeTreeVO i:list2){
			//16进制颜色码转RGB
			String colorCode = i.getColorCode();
			if(StrUtil.isNotBlank(colorCode)){
				i.setRgb(hexToRGB(i.getColorCode()));
			}

			//第三级才去计算状态
			if(!PropertyType.IdEnum.ID_4.getCode().equals(i.getParentId())){
				i.setMaintainState(Property.MaintainStateEnum.convertState(i.getIsMaintain(),i.getNextTimeMaintain(),currentDate));
			}
		}

		//形成树结构
		List<PropertyTypeTreeVO> tree = TreeUtils.createTree(
				list2,
				TreeUtils.TreeFieldEnum.ID.getCode(),
				TreeUtils.TreeFieldEnum.PARENT_ID.getCode(),
				TreeUtils.TreeFieldEnum.CHILD.getCode(),
				true
		);

		return tree;
	}

	/**
	 * 通过主键id集合查询
	 */
	@Override
	public List<PropertyType> getByIds(List<Integer> ids){
		if(CollectionUtils.isEmpty(ids)){
			return new ArrayList<>();
		}
		ids = ids.stream()
				.filter(i -> i != null && !i.equals(0l))
				.collect(Collectors.toList());
		if(CollectionUtils.isEmpty(ids)){
			return new ArrayList<>();
		}

		LambdaQueryWrapper<PropertyType> lqw = new LambdaQueryWrapper<>();
		lqw.in(PropertyType::getId,ids);
		List<PropertyType> list = this.list(lqw);
		if(CollectionUtils.isEmpty(list)){
			return new ArrayList<>();
		}

		return list;
	}

	/**
	 * 通过主键id集合查询 返回Map<对象主键id,对象实体> key为对象主键id
	 */
	@Override
	public Map<Integer,PropertyType> getMapByIds(List<Integer> ids){
		List<PropertyType> list = getByIds(ids);
		if(CollectionUtils.isEmpty(list)){
			return new HashMap<>();
		}

		Map<Integer,PropertyType> map = list.stream()
				.collect(Collectors.toMap(i -> i.getId(), Function.identity()));

		return map;
	}

	/**
	 * 通过主键id在对象Map<对象主键id,对象实体>获取对象
	 */
	@Override
	public PropertyType getByMap(Map<Integer, PropertyType> map,Integer id){
		if(MapUtil.isEmpty(map)|| NumberUtils.integerEmpty(id)){
			return null;
		}
		return map.get(id);
	}

	/**
	 * 通过主键id在对象Map<对象主键id, 对象实体>获取对象名称
	 */
	@Override
	public String getNameByMap(Map<Integer, PropertyType> map,Integer id){
		if(MapUtil.isEmpty(map)||NumberUtils.integerEmpty(id)){
			return "";
		}

		PropertyType entity = map.get(id);
		if(Objects.isNull(entity)){
			return "";
		}
		return Optional.ofNullable(entity.getName()).orElse("");
	}

	public static ColorRgbVO hexToRGB(String hexColor) {
		Color color = Color.decode(hexColor);
		int red = color.getRed();
		int green = color.getGreen();
		int blue = color.getBlue();
		return new ColorRgbVO(red, green, blue);
	}

}



